class xT_AICommand extends GameAICommand;

//=============================================================================
// Main state
//=============================================================================
AUTO state Idle
{
}

//=============================================================================
// Reaction on sensors
//=============================================================================
public function Reaction_HearNoise( float Loudness, Actor NoiseMaker, optional Name NoiseType )
{
	//`log("Reaction_HearNoise");
	//`log(self$" HearNoise from: "$NoiseMaker$" | Loudness = "$Loudness$" | NoiseType = "$NoiseType);
	if ( ( Pawn(NoiseMaker) != none ) && ( xT_Pawn(Pawn).Team != xT_Pawn(NoiseMaker).Team ) )
	{
		if (CommandList == GetActiveCommand()) if (Loudness > 0.5) CheckNoise(xT_Pawn(NoiseMaker)); // if pawn in main command state only
	}
}

public function Reaction_SeeEnemy(xT_Pawn aEnemy)
{
	//`log("Reaction_EnemyVisible");
	// first is say to self and near units that see enemy detected
	Report_EnemyDetected(aEnemy);
	Report_TeamAboutEnemy(aEnemy);
	if (Pawn != none)
	{
		if (CommandList == GetActiveCommand()) CheckForEnemyReported();
		//{
		//	if (Pawn.Weapon != none)
		//	{
		//		xT_AIController(Outer).StartAttackEnemy(aEnemy);
				
		//	}
		//	else 
		//	{
		//		xT_AIController(Outer).MoveFarFromEnemy();
		//	}
		//}
		else if (GetActiveCommand().IsA('xT_AICommand_Cmd_Move') && Pawn.IsA('xT_Pawn_Alien')) CheckForEnemyReported(); //used with Aliens only
	}
}

public function Reaction_UnderEnemyFire(xT_Pawn aEnemy)
{
	if (    (xT_AIController(Outer).GetActiveCommand().Class != class'xT_AICommand_Cmd_AttackEnemy') &&
					(!xT_AIController(Outer).GetActiveCommand().IsA('xT_AICommand_Cmd_Move'))    )
		TryToLookAtAttacker(aEnemy);
}

public function Reaction_EnemyLost()
{
	//`log("Reaction_UnderEnemyFire");
	EnemyLost();
}

//=============================================================================
// Report
//=============================================================================
protected function Report_EnemyDetected(xT_Pawn aEnemy) // also overrided for command states
{
	xT_AIController(Outer).RegisterEnemy(aEnemy);
}

protected function Report_TeamAboutEnemy(xT_Pawn aEnemy)
{
	local xT_Pawn ltmpPawn;
	local float lReportRaduis;
	local xT_AIController lController;
	local xT_AICommand lCommand;

	lReportRaduis = 2048; // radius from pawn. pawn will report all other pawns in this radius about enemy

	foreach OverlappingActors(class'xT_Pawn', ltmpPawn, lReportRaduis)
	{
		if (ltmpPawn != none)
		{
			if (xT_Pawn(Pawn).Team == ltmpPawn.Team)
			{
				if (ltmpPawn.controller != none) 
				{
					lController = xT_AIController(ltmpPawn.controller);
					lCommand = xT_AICommand(lController.GetActiveCommand());
					if (lCommand != none) lCommand.Report_EnemyDetected(aEnemy);
				}
			}
		}
	}
}

//=============================================================================
// Actions
//=============================================================================
public function CheckForEnemyReported()
{
	If (xT_AIController(Outer).GetEnemiesCount() > 0)
	{
		if (Pawn.Weapon != none) xT_AIController(Outer).StartAttackEnemy(xT_AIController(Outer).GetClosestEnemy()); //we get the closest enemy
		else 
		{
			xT_AIController(Outer).MoveFarFromEnemy();
		}
	}
}

protected function CheckNoise(xT_Pawn aPossibleEnemy)
{
	xT_AIController(Outer).CheckNoise(aPossibleEnemy);
}

protected function EnemyLost()
{
	xT_AIController(Outer).UnRegisterEnemy(xT_AIController(Outer).EnemyAim);
}

protected function TryToLookAtAttacker(xT_Pawn aAtackedByEnemy)
{
	local rotator lnewrot;
	local vector loffset;

	lnewrot.Yaw = Rand(4096);
	loffset = Vector(lnewrot);
	
	switch(Rand(1))
	{
		case 0:     xT_AIController(Outer).ChangePawnRotation(aAtackedByEnemy.Location + loffset);
		break;
		case 1:     xT_AIController(Outer).ChangePawnRotation(aAtackedByEnemy.Location - loffset);
		break;
	}
}

//=============================================================================
// Default Properties
//=============================================================================
defaultproperties
{
}
